package cn.com.nes.site.interceptor;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Repository
public class SessionInterceptor extends HandlerInterceptorAdapter {

	private List<String> allowUrls;

	public List<String> getAllowUrls() {
		return allowUrls;
	}

	public void setAllowUrls(List<String> allowUrls) {
		this.allowUrls = allowUrls;
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {// TODO

		String requestUrl = request.getRequestURI();
		for (String url : allowUrls)
		{
			if (requestUrl.endsWith(url)) {
				return true;
			}
		}
		
		Object session = WebUtils.getSessionAttribute(request, "UINFO");
		if (session != null) {
			return true;
		} else {
			//request.getRequestDispatcher("/index.jsp").forward(request,response);
			return true;
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object o,
								Exception e) throws Exception {
	}
	
}
